package com.hrt.datasender.web.monitordata;

import com.btcode.db.result.Row;
import com.btcode.exception.MsgException;
import com.btcode.orm.executer.IORMExecuter;
import com.btcode.orm.model.IModel;
import com.btcode.web.core.annotation.RequestMapping;
import com.btcode.web.core.controller.Controller;
import com.hrt.common.web.DSRequestUnit;
import com.hrt.datasender.datadao.model.BcSendPlatformDataStrategyModel;
import com.hrt.datasender.datadao.model.BcSendPlatformModel;
import com.hrt.datasender.datadao.service.BcDeviceMonitorParamService;
import com.hrt.datasender.datadao.service.BcSendPlatformDataStrategyService;
import com.hrt.datasender.datadao.service.BcSendPlatformParamService;
import com.hrt.datasender.datadao.service.BcSendPlatformService;
import com.hrt.datasender.datastorage.strategy.IStorageStrategy;
import com.hrt.datasender.datastorage.strategy.StrategyLoader;
import com.hrt.datasender.datastorage.strategy.monitordata.MonitorDataStrategy;
import com.hrt.datasender.mq.MqKit;
import com.hrt.datasender.mq.model.PacketModel;
import com.hrt.datasender.process.strategy.IProcessStrategy;
import com.hrt.datasender.process.strategy.ProcessStrategyLoader;
import com.hrt.datasender.web.operInfo.OperInfoService;
import com.hrt.db.DBManager;
import com.hrt.freemaker.FreeMakerUtil;

import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

@RequestMapping(path = "MonitorData")
public class MonitorDataController extends Controller {

    @RequestMapping(path = "getMinitorData")
    public void getMinitorData(ServletRequest resq, ServletResponse res) throws Exception {
        Map<String, String> params = DSRequestUnit.getHashMapParmS();
        MonitorDataModel result = MonitorDataService.getInstance().getMonitorData(params);
        success(result);
    }

    @RequestMapping(path = "getSourceSingleData")
    public void getSourceSingleData(ServletRequest resq, ServletResponse res) throws Exception {
        MonitorDataModel result = MonitorDataService.getInstance().getSourceSingleData();
        success(result);
    }
        
    @RequestMapping(path = "export-minitor-data")
    public void exportMinitorData(ServletRequest resq, ServletResponse res) throws Exception{
        Map<String, String> params = DSRequestUnit.getHashMapParmS();
        String dataType = params.get("dataType");
        String start = params.get("START");
        String end = params.get("END");
        String platFormID = params.get("PLATFORM_ID");
        MonitorDataModel result = MonitorDataService.getInstance().getMonitorData(params);   
        List<Row> pollList = BcSendPlatformParamService.getInstance().getPlatformParamForReport(platFormID);
        
        FreeMakerUtil freemaker = new FreeMakerUtil();
        String title = dataType + start + "至" + end;
       
        HashMap<String, Object> excelData = new HashMap<>();
        excelData.put("dataList", result);
        excelData.put("pollList", pollList);
        excelData.put("title", title);
        freemaker.exportExcelWidthFreemarkerTemp("/monitorData/tableTemp.html", null, excelData, title+".xls");
    }

    @RequestMapping(path = "reissue-minitor-data")
    public void reissueMinitorData(ServletRequest resq, ServletResponse res) throws Exception{
        Map<String, String> params = DSRequestUnit.getHashMapParmS();
        List<PacketModel> models = MonitorDataService.getInstance().reissueMinitorData(params);
        success(models);
    }

    @RequestMapping(path = "generate-minitor-data")
    public void generateMinitorData(ServletRequest resq, ServletResponse res) throws Exception{
        Map<String, String> params = DSRequestUnit.getHashMapParmS();
        MonitorDataService.getInstance().generateMinitorData(params);
        success(null);
    }
}
